package cn.hn.tx.mail.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import cn.hn.comm.LocalFiles;
import cn.hn.comm.Tools;
import cn.hn.tx.mail.app.MailSender;
import cn.hn.tx.mail.model.MailAddress;
import cn.hn.tx.mail.service.IMailService;

import com.opensymphony.xwork2.ActionSupport;

public class MailAction extends ActionSupport {

	private String method;

	private MailSender mailSender;

	private Integer importMailAddressCount;

	private IMailService mailService;

	private String splitMail;

	private long sleepTime;
	
	private String host;
	private String username;
	private String password;
	private String toaddress;
	private String fromaddress;
	

	public static void main(String[] args) {
		LocalFiles lf = new LocalFiles();
		String mail = lf.readTxtFile("D:" + File.separator + "mail.txt");
		String[] listMail=mail.split("\r\n");
		int iIndex=0;
		for(String str:listMail)
		{
			iIndex++;
			System.out.println("邮件地址"+iIndex+":"+str);
		}
	}

	public String importMailAddress() {

		importMailAddressCount = 0;

		LocalFiles lf = new LocalFiles();

		String projectRealPath = ServletActionContext.getServletContext()
				.getRealPath("/");

		// + File.separator + "mail.txt";

		List<String> mailFiles = new ArrayList<String>();
		lf.getFileName(projectRealPath + File.separator + "res"
				+ File.separator + "mail", mailFiles);

		for (String mailPath : mailFiles) {
			String mailAddress = lf.readTxtFile(mailPath);
			String[] strMails = mailAddress.trim().split(splitMail);
			System.out.println(projectRealPath);

			List<MailAddress> existMailAddress = mailService
					.queryAllMailAddress();
			List<MailAddress> newMailAddress = new ArrayList<MailAddress>();
			for (String mail : strMails) {
				boolean isImport = true;
				mail = mail.trim();
				for (MailAddress md : existMailAddress) {
					if (md.getMail_address().equals(mail)) {
						isImport = false;
						break;
					}
				}
				if (isImport) {
					MailAddress mailAR = new MailAddress();
					mailAR.setStatus(MailAddress.STATE_ENABLE);
					mailAR.setMail_address(mail);
					mailAR.setInsert_time(Tools.getCurDate(null));
					newMailAddress.add(mailAR);
				}
			}
			for (MailAddress md : newMailAddress) {
				mailService.insertMailAddress(md);
			}
			importMailAddressCount += newMailAddress.size();
		}

		return SUCCESS;
	}

	public String status() {
		method = "status";
		return SUCCESS;
	}

	public String stopSendMail() {
		method = "stopSendMail";
		mailSender.stopSendMail();
		return SUCCESS;
	}

	public String sendMail() {
		method = "sendMail";
		mailSender.sendMail();
		return SUCCESS;
	}

	public String sendTestMail() {
//		method = "已发送测试邮件";
//		mailSender.sendTestMail();
		return SUCCESS;
	}
	
	public String confirmSendTestMail()
	{
		method = "已发送测试邮件";
		mailSender.sendTestMail(host,username,password,toaddress,fromaddress);
		return SUCCESS;
	}
	

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public MailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public Integer getImportMailAddressCount() {
		return importMailAddressCount;
	}

	public void setImportMailAddressCount(Integer importMailAddressCount) {
		this.importMailAddressCount = importMailAddressCount;
	}

	public IMailService getMailService() {
		return mailService;
	}

	public void setMailService(IMailService mailService) {
		this.mailService = mailService;
	}

	public String getSplitMail() {
		return splitMail;
	}

	public void setSplitMail(String splitMail) {
		this.splitMail = splitMail;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getToaddress() {
		return toaddress;
	}

	public void setToaddress(String toaddress) {
		this.toaddress = toaddress;
	}

	public String getFromaddress() {
		return fromaddress;
	}

	public void setFromaddress(String fromaddress) {
		this.fromaddress = fromaddress;
	}

	public void setSleepTime(long sleepTime) {
		this.sleepTime = sleepTime;
	}

	public long getSleepTime() {
		if (mailSender.getState() == mailSender.STATE_SENDING) {
			return System.currentTimeMillis() - mailSender.getSleepStartTime();
		} else {
			return 0;
		}
	}

}
